#!/usr/bin/env python3
# -*- coding: utf-8 -*-


from datetime import datetime
import pymysql

Second = 1000
minute = 60 * Second
hour = 60 * minute
sceneId = 'andr-hunfu-scene-jiuyou-10.135.158.161-001'


def getMysqlCon():
    # return pymysql.connect(host="localhost", port=11001, user='badperson', passwd="1q2w3e4r", db="bpActivity_9123", charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
    return pymysql.connect(host="172.16.1.65", port=3306, user='badperson', passwd="1q2w3e4r", db="test2", charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)


def openNewMatch():
    prepare2BeginTime = 10 * minute
    begin2EndTime = 10 * minute
    end2ShowTime = 10 * minute

    conn = getMysqlCon()

    newId = activityMainPartitionLogic(conn, next10MinuteTime())  # 1.
    jiuyouMainSceneIdLogic(conn, newId)
    jiuyouMainServerLogic(conn, newId)

    conn.close()


def jiuyouMainServerLogic(conn, newId):
    serverIds = [998, 9996]
    for serverId in serverIds:
        jiuyouMainServerLogic0(conn, newId, serverId)


def jiuyouMainServerLogic0(conn, newId, serverId):
    sql = '''insert into activity_jiuyou_main_partition_server(`serverId`, `dictPartitionId`, `platformId`) values(%d, %d, 1)''' % (serverId, newId)
    cursor = conn.cursor()
    cursor.execute(sql)
    conn.commit()


def jiuyouMainSceneIdLogic(conn, newId):
    sql = '''insert into activity_jiuyou_main_partition_sceneId(`dictPartitionId`, `sceneConfigId`, `sceneIndex`) values(%d, '%s', 1)''' % (newId, sceneId)
    cursor = conn.cursor()
    cursor.execute(sql)
    conn.commit()


def activityMainPartitionLogic(conn, startTime):
    cursor = conn.cursor()

    sql = 'select id, partitionId, jie, prepareTime, prepareToBeginTime, beginToEndTime, endToShowEndTime, describeInfo from activity_jiuyou_main_partition order by id desc limit 1'
    cursor.execute(sql)
    result = cursor.fetchone()
    # print(result)

    # 插入需要的数据
    jie = result['jie'] + 1
    sql = '''insert into activity_jiuyou_main_partition(`partitionId`, `jie`, `prepareTime`, `prepareToBeginTime`, `beginToEndTime`, `endToShowEndTime`, `describeInfo`) values(%d, %d, %d, %d, %d, %d, '%s')''' \
          % (result['partitionId'], jie, startTime, result['prepareToBeginTime'], result['beginToEndTime'], result['endToShowEndTime'], '9996test' + str(jie))
    print(sql)
    cursor.execute(sql)
    conn.commit()

    sql = 'select id, partitionId, jie, prepareTime, prepareToBeginTime, beginToEndTime, endToShowEndTime, describeInfo from activity_jiuyou_main_partition order by id desc limit 1'
    cursor.execute(sql)
    result = cursor.fetchone()

    return result['id']


def next10MinuteTime():
    now = datetime.now()
    minute = int(now.minute / 10 + 1) * 10
    hour = now.hour
    if minute == 60:
        minute = 0
        hour = hour + 1
    nextDate = datetime(now.year, now.month, now.day, hour, minute, 0)
    return int(nextDate.timestamp() * 1000)


if __name__ == '__main__':
    openNewMatch()





